from django.shortcuts import render
from django.http import HttpRequest,HttpResponseRedirect
# Create your views here.
from app import models
# 验证登录装饰器
def check_login(func):
    def warpper(request,*args,**kwargs):
        is_login=request.session.get('IS_LOGIN',False)
        if is_login:
            return func(request,*args,**kwargs)
        else:
            return HttpResponseRedirect("/login")
    return warpper
# 购物车
@check_login
def cart_view(request):
    if request.method=="POST":
        uid=request.session['user_id']
        return HttpRequest({"status":1,"msg":"添加购物车成功"})
    elif request.method=="GET":
        uid =request.session['user_id']
        list=[]
        cart_list=models.Cart.objects.filter(user_id=uid)
# 登录
def login_view(request):
    if request.method=="POST":
        user_name=request.POST.get('user_name','')
        pass_wrod=request.POST.get('pwd','')
        user = models.User.objects.get(user_name=user_name,pwd=pass_wrod)
        if user:
            if pass_wrod==user.pwd:
                request.session['IS_LOGIN']=True
                request.session['user_name'] = user_name
                request.session['user_id'] = user.id
                return render(request,'index.html',{'user_name':user_name})
            else:
                return render(request,'login.html',{'error':'用户名或密码错误'})
        else:
            return render(request,'login.html',{'error':'用户名不存在'})
    elif request.method=='GET':
        return render(request,'login.html')
    else:
        return render(request, 'login.html')

# 退出登录
def login_out(request):
    request.session.flush()
    return render(request,'login.html')

# 注册
def register_view(request):
    if request.method=='GET':
        return render(request,'register.html')
    elif request.method=='POST':
        user_name=request.POST.get('user_name','')
        pass_wrod_1 = request.POST.get('pwd', '')
        pass_wrod_2 = request.POST.get('cpwd', '')
        phone =request.POST.get('phone','')
        if models.User.objects.filter(user_name=user_name):
            return render(request,'register.html',{'error':'用户已存在'})
        if pass_wrod_1!=pass_wrod_2:
            return render(request,'register.html',{'error':'两次密码输入不一致'})
        user=models.User()
        user.user_name=user_name
        user.pwd=pass_wrod_1
        user.phone=phone
        user.save()
        return render(request,'login.html')
    else:
        return render(request,'register.html')

# 首页
def index_view(request):
    if request.method=="GET":
        goods_type=models.Type.ordering.all()
        goods_dict={}
        for type in goods_type:
            goods_list=models.Goods.object.filter(type__id=type.id)[:4]
            goods_dict[type.type_name]=goods_list
    print(goods_dict)
    return render(request,'index.html',{"goods_list":goods_dict,"goods_type":goods_type})
# 商品详情
def goodDetail(request,id):
    good_detail=models.Goods.object.get(id=id)
    return render(request,"detail.html",{"good":good_detail,"user_name":request.session.get("user_name",None)})